package com.atguigu.spzx.pay.controller;

//import cn.hutool.http.server.HttpServerRequest;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.atguigu.spzx.common.exception.SpzxException;
import com.atguigu.spzx.model.entity.order.OrderInfo;
import com.atguigu.spzx.model.entity.order.OrderItem;
import com.atguigu.spzx.model.entity.pay.PaymentInfo;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.order.client.OrderClient;
import com.atguigu.spzx.pay.config.AlipayConfig;
import com.atguigu.spzx.pay.service.PaymentInfoService;
import com.atguigu.spzx.product.client.ProductClient;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Collectors;

@RequestMapping("/api/order/alipay")
@Controller
@Tag(name = "支付模块")
public class PayController {

    @Resource
    OrderClient orderClient;
    @Resource
    PaymentInfoService paymentInfoService;

    //异步回调接口：接收支付宝回传的用户支付的信息，异步接口中需要判断 数据是否安全、支付宝支付是否成功、支付金额是否正确
    //              更新订单的支付状态、更新库存、保存交易日志
    @Operation(summary = "用户支付成功的同步回调:跳转到支付成功页面(订单列表页)")
    @ResponseBody
    @PostMapping("/notify_url")
    public String notifyUrl(@RequestParam Map<String, String> params) throws UnsupportedEncodingException, AlipayApiException {
        return paymentInfoService.notifyUrl(params);
    }


    //?charset=UTF-8&out_trade_no=1717815274826240000&method=alipay.trade.wap.pay.return&total_amount=1111.00&sign=C6QAkl8gCaDOZOu%2FJDlWNyvZBHGVq5o%2BP1CpcI%2BjAqDLYoVwuRwOHZ6Z3yTMnJQ7Q9pMH94L55VocrC7DQGoH9G0%2BXpNJZDDK1G8CPKO9ut1WdaQxZ4jeFVg%2FNuqQSMNd9qBdWqf%2BT7h3rrbtshcgzkuKtOd3PEJMk53bUrxSJOKugpR2D6IAKPvxGPlCuO1gYkrLy6H17Qu73dbwTrvAQXF3eKtsQmTAFu4i9mT7x2vTvQZGULBtRqVheR57v%2FtSvKleFWxNtaUt%2FxELGgHMUOiZct2HhGlZz3XMUcOYHNXKDMrhSxCCjee733VXKKOl1bPt7dssZQk%2FXabThXD6Q%3D%3D&trade_no=2023102822001475950501185426&auth_app_id=9021000126647739&version=1.0&app_id=9021000126647739&sign_type=RSA2&seller_id=2088721011047335&timestamp=2023-10-28+11%3A50%3A16
    @Operation(summary = "用户支付成功的同步回调:跳转到支付成功页面(订单列表页)")
    @GetMapping("/return_url")
    public String returnUrl(HttpServletRequest request) throws UnsupportedEncodingException, AlipayApiException {

        return paymentInfoService.returnUrl(request);
    }

    @ResponseBody //controller注解+该注解 可以将结果转为json响应
    @Operation(summary = "支付宝下单接口")
    @GetMapping("/submitAlipay/{orderNo}")
    public Result submitAlipay(@PathVariable("orderNo") Long orderNo) {

        return paymentInfoService.submitAlipay(orderNo);

    }

}
